/**
 * 
 */
$(function() {
	var websocket;
	if ('WebSocket' in window) {
		websocket = new WebSocket("ws://"+window.location.host+"/wsServer");
	} else if ('MozWebSocket' in window) {
		websocket = new MozWebSocket("ws://"+window.location.host+"/wsServer");
	} else {
		alert("websocket创建失败,将无法使用通知功能,请更换浏览器!或从主页下载");
	}
	websocket.onopen = function(evnt) {
		console.log("ws open ....");
        var _ws = websocket;
		//websocket.send("message");
        if(websocket.readyState==websocket.OPEN){
            console.log("init heart beat in notice");
            setInterval(function(){
                _ws.send('hb');
            },'20000');//20s发送一次
        }
	};
	websocket.onmessage = function(evnt) {
		var count = parseInt(evnt.data);
		if (count) {
			$("#userInfo").popover({
				placement: 'bottom',
				title: '通知提示',
				content: '您有新的通知请查看',
				delay: {show: 500, hide: 100}
			}).popover('show');
			var h = $("#notifyNum").html();//未读的个数
			if(parseInt(h)){ 
				count += parseInt(h);//累加
			}
			$("#notifyNum").show().html(count);
			$("#notifyNum2").show().html(count);
			$("#userInfo").click(function(){
				$(this).popover('hide');
			});
			//5秒后销毁弹出框
			setTimeout(function(){$("#userInfo").popover('destroy');},5000);
		}
	};
	websocket.onerror = function(evnt) {
		console.log("ws error ....");
	};
	websocket.onclose = function(evnt) {
		console.log("ws close ....");
		websocket.close();
	};
});